package com.xiaoyu.izf.util;

/**
 * ==========================================
 * @(#)DateTimeUtil.java	2003/07/12
 * @author              playp
 * @version             1.0
 * ==========================================
 */
 
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {
	
	public static String getDateTime2Str() {
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dt=sdf.format(new Date());
        return dt;
	}
	public static String getDateTime2Str2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}	
	public static String getDateTime2Str3() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}	
	public static String getDateTime2Str3S4() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getDate2Str() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getDate2Str2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}	
	public static String getDate2StrM() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getDate2StrM2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getYear2Str() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getHourStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getMonth2Str() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	public static String getDay2Str() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
        String shortdt=sdf.format(new Date());
        return shortdt;
	}
	
	public static Date getDateTime(String strDateTime){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date strToDateTime = null;
		try {
		    strToDateTime=formatter.parse(strDateTime);
		} catch(ParseException e) {}
		return strToDateTime;
	}
	public static Date getDate(String strDate){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date strToDate = null;
		try {
		    strToDate=formatter.parse(strDate);
		} catch(ParseException e) {}
		return strToDate;
	}
	public static String getDate2(String strDate){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		String strToDate = null;
		try {
		strToDate = formatter.format(formatter.parse(strDate)).toString();		    
		} catch(ParseException e) {}
		return strToDate;
	}
	
	public static Date getDateTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String dateString=formatter.format(new Date()); 
		Date nowDate = null;
		try {
		    nowDate = formatter.parse(dateString);
		} catch(ParseException e) {}
		return nowDate; 
	}
	public static Date getDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		String dateString=formatter.format(new Date()); 
		Date nowDateShort = null;
		try {
		    nowDateShort = formatter.parse(dateString);
		} catch(ParseException e) {}
		return nowDateShort; 
	}
	

	public static long getDateDepartNum(String sDate,String eDate){
		Date d1 = DateTimeUtil.getDate(sDate);
		Date d2 = DateTimeUtil.getDate(eDate);
		long dayNum = (long)(d2.getTime()-d1.getTime())/(1000*60*60*24);	
		return dayNum; 
	}
	
	public static String getDate2StrByNum(String dateStr,int num){
		Calendar c = Calendar.getInstance();   
		c.setTime(DateTimeUtil.getDate(dateStr));  
		c.add(Calendar.DATE,num);
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");   			
		String st  = sd.format(c.getTime());
		return st;
	}
	
	public static long getNumBySeperateDateStr(String startDateStr,String endDateStr){
		long rowe = (long)(DateTimeUtil.getDate(endDateStr).getTime()-DateTimeUtil.getDate(startDateStr).getTime())/(1000*60*60*24);
		return rowe;
	}
	
	/**
	 * 获取2个时间的差，毫秒
	 * @param startDateStr
	 * @param endDateStr
	 * @return
	 */
	public static long getNumBySeperateDateTimeStr(String startDateStr,String endDateStr){
		long rowe = (long)(DateTimeUtil.getDateTime(endDateStr).getTime()-DateTimeUtil.getDateTime(startDateStr).getTime())/(1000*60*60*24);
		return rowe;
	}
	
	/**
	 * 获取2个时间的差，毫秒
	 * 结束时间是现在
	 * @param startDateStr
	 * @param endDateStr
	 * @return
	 */
	public static long getNumBySeperateDateTimeStr(String startDateStr){
		long rowe = (long)(new Date().getTime()-DateTimeUtil.getDateTime(startDateStr).getTime());
		return rowe;
	}
	
	/**
	 * 获取Unix时间戳时间
	 * @param dateTime
	 * @return
	 */
	public static long getUnixTime(String dateTime){
		long t = 0;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
		Date date;
		try {
			date = df.parse(dateTime);
			t=date.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		return t;
	}
	
	public static String getWeekByDate(String date){
		 Calendar c = Calendar.getInstance(java.util.Locale.CHINA);
		  String[] sp = date.split("-");
		  c.set(Calendar.YEAR,Integer.parseInt(sp[0]));
		  c.set(Calendar.MONTH,Integer.parseInt(sp[1])-1);
		  c.set(Calendar.DATE,Integer.parseInt(sp[2]));
		  
		  int wd = c.get(Calendar.DAY_OF_WEEK);
		  String x = "";
		  switch(wd){
		  case 1:x="锟斤拷锟斤拷锟斤拷";break;
		  case 2:x="锟斤拷锟斤拷一";break;
		  case 3:x="锟斤拷锟节讹拷";break;
		  case 4:x="锟斤拷锟斤拷锟斤拷";break;
		  case 5:x="锟斤拷锟斤拷锟斤拷";break;
		  case 6:x="锟斤拷锟斤拷锟斤拷";break;
		  case 7:x="锟斤拷锟斤拷锟斤拷";break;
		  }
		  return x;
	}
	
	public static String getDate2Str(java.sql.Date date){
		if(date!=null){
			Date strDate = new Date(date.getTime());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        String shortdt=sdf.format(strDate);
	        return shortdt;
		}else{
			return "";
		}
	}
	
	public static String getDate2StrYYmmDD(Date date){
		if(date!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	        String shortdt=sdf.format(date);
	        return shortdt;
		}else{
			return "";
		}
	}
	
	public static String getDate2StrYYmmDDHHmmss(Date date){
		if(date!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String shortdt=sdf.format(date);
	        return shortdt;
		}else{
			return "";
		}
	}
	
	public static String getDateStrToStr(String date,String fomat){
		SimpleDateFormat formatter = new SimpleDateFormat(fomat); 
		String strToDate = null;
		try {
		strToDate = formatter.format(formatter.parse(date)).toString();		    
		} catch(ParseException e) {}
		return strToDate;
	}
	
	public static Date getDateByDate(Date date){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
		String dateString=formatter.format(date); 
		Date nowDateShort = null;
		try {
		    nowDateShort = formatter.parse(dateString);
		} catch(ParseException e) {}
		return nowDateShort; 
	}
	
	public static boolean comparisonDate(Date before,Date end){
		boolean ret = false;
		if(end.after(before)){
			ret=true;
		}
		return ret;
	}
	
	public static String getDateByString(String dateStr){
		Calendar c=Calendar.getInstance();
		c.setTime(getDate(dateStr));
		return c.get(c.YEAR)+"锟斤拷"+(c.get(c.MONTH)+1)+"锟斤拷"+c.get(c.DAY_OF_MONTH)+"锟斤拷";
	}
	
	public static String getDateByString2(String dateStr){
		Calendar c=Calendar.getInstance();
		c.setTime(getDateTime(dateStr));
		String minuteStr;
		int minute = c.get(c.MINUTE);
		minuteStr = minute+"锟斤拷";
		return c.get(c.YEAR)+"锟斤拷"+(c.get(c.MONTH)+1)+"锟斤拷"+c.get(c.DAY_OF_MONTH)+"锟斤拷"+c.get(c.HOUR_OF_DAY)+"时"+minuteStr;
	}
	public static String getDateByString3(String dateStr){
		Calendar c=Calendar.getInstance();
		c.setTime(getDateTime(dateStr));
		String minuteStr;
		int minute = c.get(c.MINUTE);
		minuteStr = minute+"锟斤拷";
		return (c.get(c.MONTH)+1)+"锟斤拷"+c.get(c.DAY_OF_MONTH)+"锟斤拷"+c.get(c.HOUR_OF_DAY)+"时";
	}
	
	public static String getStrDateAfterOrBefore(int num,String dateStr){
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
	        date = new SimpleDateFormat("yy-MM-dd").parse(dateStr);
	    } catch (ParseException e) {
	        e.printStackTrace();
	    }
	    c.setTime(date);
	    c.add(Calendar.DAY_OF_MONTH, num);
        String day = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	    return day;
	}
	
	 //获取日期   年\月\周
	  public static String getStrDateStyle(){
		  	Calendar c=Calendar.getInstance();
		    c.setFirstDayOfWeek(Calendar.MONDAY);
			c.setTime(new Date());
			return c.get(c.YEAR)+"\\"+(c.get(c.MONTH)+1)+"\\"+c.get(c.WEEK_OF_YEAR);
	  }
	  
	  public static long getCurrentTimeMillis(){
		  long time = System.currentTimeMillis();
		  String hour = getHourStr();
		  if("12".equals(hour)){
			  time = time-12*60*60*1000;
		  }
		  return time;
	  }
}